package messagesvc.websocket.config;

import cn.ucloud.microsf.messagesvc.websocket.handler.MessageHandler;
import cn.ucloud.microsf.messagesvc.websocket.interceptor.MessageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * 开启 WebSocket 支持
 * Spring 封装过的方式
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

  @Autowired
  private MessageHandler messageHandler;
  @Autowired
  private MessageInterceptor messageInterceptor;

  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry
        .addHandler(messageHandler, "msg/connect")
        .addInterceptors(messageInterceptor)
        .setAllowedOrigins("*");
  }
}
